//
//  HCYOGVillage.m
//  YOGProj
//
//  Created by liyun on 14-7-21.
//  Copyright (c) 2014年 qkong. All rights reserved.
//

#import "HCYOGVillage.h"
#import "HCMacroDefinition.h"
#import "HCYOGGuideDOwnload.h"
#import "HCYOGvillageMap.h"
#import "HCViewController.h"

@interface HCYOGVillage ()<UITableViewDataSource,UITableViewDelegate>
{
    NSArray *listArray;
    UITableView *villageTable;
    HCViewController *instance2;
}

@end

@implementation HCYOGVillage

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateTable) name:@"HCVillage" object:nil];
    }
    return self;
}

- (void)updateTable
{
    if(villageTable)
        [villageTable reloadData];
}

- (void)viewWillAppear:(BOOL)animated
{
    if(villageTable)
        [villageTable reloadData];
}

- (void)navigationShowLeft
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = NSLocalizedString(@"HC青奥村", @"");
    
    self.view.backgroundColor = [UIColor whiteColor];
    
//    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
//    button.frame     = CGRectMake(0, 0, 30, 30);
//    [button setBackgroundImage:[UIImage imageWithContentsOfFile:[BUNDLE pathForResource:@"HCback" ofType:@"png"]] forState:UIControlStateNormal];
//    [button addTarget:self
//               action:@selector(navigationShowLeft)
//     forControlEvents:UIControlEventTouchUpInside];
//    UIBarButtonItem *barItemleft          = [[UIBarButtonItem alloc]initWithCustomView:button];
//    self.navigationItem.leftBarButtonItem = barItemleft;
    
    
    villageTable                = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 378)
                                                                style:UITableViewStylePlain];
    villageTable.delegate       = self;
    villageTable.dataSource     = self;
    villageTable.scrollEnabled = NO;
    villageTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view                  addSubview:villageTable];
    
    listArray = @[NSLocalizedString(@"HC青奥村地图", @""),
                  NSLocalizedString(@"HC餐厅服务", @""),
                  NSLocalizedString(@"HC宗教服务", @""),
                  NSLocalizedString(@"HC健身训练服务", @""),
                  NSLocalizedString(@"HC青奥村广场", @""),
                  NSLocalizedString(@"HC交通服务", @"")];
    
    NSLog(@"listArray-->%@",listArray);
}

#pragma mark - Table view data source
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 63;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return listArray.count ;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleGray;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    
    NSArray *subArr = [[NSArray alloc] initWithArray:cell.contentView.subviews];
    for (UIView *temp in subArr)
        [temp removeFromSuperview];
    
    UIImageView *line = [[UIImageView alloc] initWithFrame:CGRectMake(0, 62, 320, 0.5)];
    line.backgroundColor = TABLE_LINE;
    [cell.contentView addSubview:line];
    
    UIImageView *icon = [[UIImageView alloc] initWithFrame:CGRectMake(14, 14, 35, 35)];
    NSString *str = [NSString stringWithFormat:@"HCYOGVillage%d",indexPath.row];
    icon.image = [UIImage imageWithContentsOfFile:[BUNDLE pathForResource:str ofType:@"png"]];
    [cell.contentView addSubview:icon];
    
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(54, 16, 260, 30)];
    title.text = listArray[indexPath.row];
    title.font = NORMAL_FONT(17.0f);
    title.backgroundColor = [UIColor clearColor];
    [cell.contentView addSubview:title];
    
    UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(270, 26, 11, 11)];
    image.image = [UIImage imageWithContentsOfFile:[BUNDLE pathForResource:@"HCRedCircle" ofType:@"png"]];
    [cell.contentView addSubview:image];
    
    NSString *keyStr = [NSString stringWithFormat:@"HCSHOW%d",indexPath.row];
    int flag = [[[NSUserDefaults standardUserDefaults] objectForKey:keyStr] intValue];
    
    switch (indexPath.row)
    {
        case 1:
        {
            if(flag == 0)
                image.hidden = YES;
            else
                image.hidden = NO;
        }
            break;
        case 2:
        {
            if(flag == 0)
                image.hidden = YES;
            else
                image.hidden = NO;
        }
            break;
        case 3:
        {
            if(flag == 0)
                image.hidden = YES;
            else
                image.hidden = NO;
        }
            break;
        case 4:
        {
            if(flag == 0)
                image.hidden = YES;
            else
                image.hidden = NO;
        }
            break;
        default:
            image.hidden = YES;
            break;
    }
        
        
    
     cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.row == 0)
    {
        HCYOGvillageMap *instance = [[HCYOGvillageMap alloc] init];
        [self.navigationController pushViewController:instance animated:YES];
    }
    else if(indexPath.row == 5)
    {
        HCYOGGuideDOwnload *instance = [[HCYOGGuideDOwnload alloc] init];
        [self.navigationController pushViewController:instance animated:YES];
    }
    else
    {
        NSString *keyStr = [NSString stringWithFormat:@"HCSHOW%d",indexPath.row];
        [[NSUserDefaults standardUserDefaults] setObject:@"0" forKey:keyStr];
        [villageTable reloadData];
        
        if(!instance2)
        {
            instance2 = [[HCViewController alloc] init];
        }
        instance2.ibeaconType = indexPath.row;
        [self.navigationController pushViewController:instance2 animated:YES];
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
